package com.qf.flash.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.qf.flash.R;
import com.qf.flash.constant.Constants;
import com.qf.flash.model.RegionBean;
import com.qf.flash.mvp.contract.LocationContract;
import com.qf.flash.mvp.contract.RegionContract;
import com.qf.flash.mvp.presenter.LocationPresenterImpl;
import com.qf.flash.mvp.presenter.RegionPresenter;
import com.qf.flash.ui.home.adapter.RegionLeftItem;
import com.qf.flash.ui.home.adapter.RegionRightItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.permission.PermissionManager;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2020/2/23 on 16:16
 * 描述:
 * 作者:戴旭东
 */
public class ChangeRegionActivity extends BaseActivity implements LocationContract.View, RegionContract.View {
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_region)
    TextView tvRegion;
    @BindView(R.id.rv_left)
    RecyclerView rvLeft;
    @BindView(R.id.rv_right)
    RecyclerView rvRight;

    private RVAdapter<RegionBean> mLeftAdapter;
    private RVAdapter<RegionBean> mRightAdapter;

    private LocationPresenterImpl mLocatePresenter;
    private RegionPresenter mRegionPresenter;

    private long adCode;
    public static void start(Activity activity, String cityName, long adCode)
    {
        Intent intent = new Intent(activity, ChangeRegionActivity.class);
        intent.putExtra("adCode", adCode);
        intent.putExtra("cityName", cityName);
        activity.startActivity(intent);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_city;
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("切换城市");
        adCode  = getIntent().getLongExtra("adCode", 0);
        String cityName = getIntent().getStringExtra("cityName");
        if (!TextUtils.isEmpty(cityName)) {
            tvRegion.setText(cityName);
        }
        initLeftRv();
        initRightRv();
        mRegionPresenter.getRegionList();
    }

    @Override
    protected void initInject() {
        mLocatePresenter = new LocationPresenterImpl(this);
        mRegionPresenter = new RegionPresenter();
        initPresenters(mLocatePresenter, mRegionPresenter);
    }

    private void initLeftRv() {
        rvLeft.setLayoutManager(new LinearLayoutManager(this));
        mLeftAdapter = new RVAdapter<>(new RegionLeftItem());
        mLeftAdapter.setSingleChosenPosition(0);
        rvLeft.setAdapter(mLeftAdapter);
        mLeftAdapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mLeftAdapter.setSingleChosenPosition(position);
                mLeftAdapter.notifyDataSetChanged();

                List<RegionBean> sub = mLeftAdapter.getData(position).getSub();
                mRightAdapter.setDatas(sub);
            }
        });

    }
    private void initRightRv() {
        rvRight.setLayoutManager(new LinearLayoutManager(this));
        mRightAdapter = new RVAdapter<>(new RegionRightItem());
        rvRight.setAdapter(mRightAdapter);
        mRightAdapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                RegionBean data = mRightAdapter.getData(position);
                RegionBean parent = mLeftAdapter.getData(mLeftAdapter.getSingleChosenPosition());
                data.setPid(parent.getId());
                postEvent(data);
                finish();
            }
        });

    }

    private void requestPermission() {
        new PermissionManager(this)
                .addPermission(Constants.LOCATE_PERMISSION)
                .addListener(new PermissionManager.OnPermissionListener() {
                    @Override
                    public void onSuccess(List<String> permissions) {
                        showLoadingDialog("定位中...", true);
                        mLocatePresenter.startOncetLocation();
                    }

                    @Override
                    public void onFailed(List<String> permissions) {
                        FlashLib.showToast("权限拒绝");
                    }
                });
    }



    @OnClick({R.id.tv_top_back, R.id.tv_reLocate})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_reLocate:
                requestPermission();
                break;
        }
    }

    @Override
    public void onLocationSuccess(AMapLocation location) {
        dismissLoadingDialog();
        tvRegion.setText(location.getDistrict());
        EventBus.getDefault().post(location);
    }

    @Override
    public void onLocationFailed(String msg) {
        dismissLoadingDialog();
        FlashLib.showToast("定位失败");
    }

    @Override
    public void onError(ApiException e) {

    }

    @Override
    public void setRegionListData(List<RegionBean> regionBeans) {
        if (regionBeans != null &&regionBeans.size() > 0) {
            mLeftAdapter.setSingleChosenPosition(0);
            mLeftAdapter.setDatas(regionBeans);
            mRightAdapter.setDatas(regionBeans.get(0).getSub());
        }

    }
}
